package com.xiaoshuidi.cloud.module.finance.job.invoice;

import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xiaoshuidi.cloud.framework.tenant.core.job.TenantJob;
import com.xiaoshuidi.cloud.module.contract.api.ContractRentOrderApi;
import com.xiaoshuidi.cloud.module.finance.dal.dataobject.invoice.InvoiceDO;
import com.xiaoshuidi.cloud.module.finance.dal.mysql.invoice.BaseInvoiceMapper;
import com.xiaoshuidi.cloud.module.finance.util.invoice.Base64Util;
import com.xiaoshuidi.cloud.module.finance.util.invoice.BwServiceUtil;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;
import cn.hutool.json.JSONUtil;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Component
@TenantJob
@RefreshScope
public class RedInvoiceJob {
    @Resource
    private BaseInvoiceMapper invoiceMapper;
    @Resource
    private ContractRentOrderApi contractRentOrderApi;


    /**
     * 红字发票确认单状态查询
     */
    @XxlJob("fpChConfirmStatusSearchJob")
    public void fpChConfirmStatusSearchJob() {
        log.info("=================== 红字发票确认单状态查询 - 执行开始 ===================");
        LambdaQueryWrapper<InvoiceDO> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(InvoiceDO::getStatus, "7");
        wrapper.isNotNull(InvoiceDO::getRedSeqNo);
        List<InvoiceDO> invoiceList = invoiceMapper.selectList(wrapper);
        for (InvoiceDO invoice : invoiceList) {
            Map<String, String> map = new HashMap<>();
            map.put("djbh", invoice.getRedSeqNo());
            String result = BwServiceUtil.fpChConfirmStatusSearch(map);
            JSONObject jsonObject = JSONUtil.parseObj(result);
            if (StringUtils.equals(jsonObject.get("code").toString(), "-1")) {
                invoice.setStatus("9");
                invoice.setErrorReason(jsonObject.get("renson").toString());
                contractRentOrderApi.updateOrderByRushRed(invoice.getId(), 9);
            } else {
                invoice.setStatus("8");
                contractRentOrderApi.updateOrderByRushRed(invoice.getId(), 8);
            }
            invoiceMapper.updateById(invoice);
        }

        log.info("=================== 红字发票确认单状态查询 - 执行结束 ===================");
    }


    /**
     * 红字开具结果查询
     */
    @XxlJob("fpKjStatusSearchJob")
    public void fpKjStatusSearchJob() {
        log.info("=================== 红字开具结果查询 - 执行开始 ===================");
        LambdaQueryWrapper<InvoiceDO> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(InvoiceDO::getStatus, "8");
        wrapper.isNotNull(InvoiceDO::getRedSeqNo);
        List<InvoiceDO> invoiceList = invoiceMapper.selectList(wrapper);
        for (InvoiceDO invoice : invoiceList) {
            Map<String, String> map = new HashMap<>();
            map.put("djbh", invoice.getRedSeqNo());
            String result = BwServiceUtil.fpChKjStatusSearch(map);
            JSONObject jsonObject = JSONUtil.parseObj(result);
            if (StringUtils.equals(jsonObject.get("code").toString(), "-1")) {
                invoice.setStatus("9");
                invoice.setErrorReason(jsonObject.get("renson").toString());
                contractRentOrderApi.updateOrderByRushRed(invoice.getId(), 9);
            } else {
                invoice.setStatus("4");
                contractRentOrderApi.updateOrderByRushRed(invoice.getId(), 4);
            }
            invoiceMapper.updateById(invoice);
        }
        log.info("=================== 红字开具结果查询 - 执行结束 ===================");
    }


}
